package com.hairline.controller;


import com.hairline.bean.bo.AdminDeleteBo;
import com.hairline.bean.bo.AdminUpdateBo;
import com.hairline.bean.vo.AdminUpdateVo;
import com.hairline.aspect.AdminOperationLog;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.vo.RoleOptionsVo;
import com.hairline.bean.bo.AdminCreateBo;
import com.hairline.bean.vo.AdminCreateVo;
import com.hairline.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fenglin on 2020/9/1 9:53 PM
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    //http://182.92.235.201:8083/admin/role/options
//    @GetMapping("role/options")
//    public BaseRespVo roleOption(){
//        List<RoleOptionsVo> roleOptionsVoList = adminService.roleOptions();
//        return BaseRespVo.ok(roleOptionsVoList);
//    }

    @GetMapping("admin/list")
    public BaseRespVo adminList(int page, int limit, String sort, String order,String username){
        Map<String, Object> map = adminService.adminList(page, limit, sort, order, username);

        return BaseRespVo.ok(map);
    }

    //接收数据 {"username":"qqqqq1","password":"qqqqq1","roleIds":[2,1]}
    @AdminOperationLog
    @PostMapping("admin/create")
    public BaseRespVo adminCreate(@RequestBody AdminCreateBo adminCreateBo){
        AdminCreateVo adminCreateVo = adminService.adminCreate(adminCreateBo);
        if(adminCreateVo == null){
            return BaseRespVo.fail("管理员已经存在");
        }
        return BaseRespVo.ok(adminCreateVo);
    }

    //接收数据：id：Integer
    @AdminOperationLog
    @PostMapping("admin/update")
    public BaseRespVo adminUpdate(@RequestBody AdminUpdateBo adminUpdateBo){
        AdminUpdateVo adminUpdateVo = adminService.adminUpdate(adminUpdateBo);
        return BaseRespVo.ok(adminUpdateVo);
    }


    @AdminOperationLog
    @PostMapping("admin/delete")
    public BaseRespVo adminDelete(@RequestBody AdminDeleteBo adminDeleteBo){
        int i = adminService.adminDelete(adminDeleteBo);
        if(i == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

}
